package com.sgdh.netty.handler;


import com.sgdh.netty.constant.CaptureImage;
import com.sgdh.netty.constant.MyPanel;
import com.sgdh.netty.constant.RemoteDesktop;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ClientHandler extends ChannelInboundHandlerAdapter {
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		CaptureImage captureImage = (CaptureImage) msg;
		MyPanel m = RemoteDesktop.panelMap.get(String.valueOf(captureImage.getPanelId()));
		m.display(captureImage.getContent());
		String message = String.format("%20s", "ACK");
		String panelId= String.format("%20s", String.valueOf(captureImage.getPanelId()));
		ctx.writeAndFlush(message+panelId + '\n');
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
	
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.err.println("An existing connection was forcibly closed by the remote host");
	}
}
